package com.sl.plugin.step.json;

import cn.hutool.core.util.StrUtil;
import com.bazaarvoice.jolt.Chainr;
import com.sl.core.utils.JsonUtil;
import com.sl.core.engine.step.ProcessCompService;
import com.sl.core.engine.step.impl.AbstractProcessComp;
import lombok.SneakyThrows;

import java.util.List;
import java.util.Map;

/**
 *
 */
public class JsonOutputComp extends AbstractProcessComp<Object, Object> implements ProcessCompService {


    public JsonOutputComp(JsonOutputCompDescriptor abstractProcessCompDescriptor) {
        super(abstractProcessCompDescriptor);
    }


    @SneakyThrows
    @Override
    public Object input(Object driver) {

        String jolt = this.meta.getStrAttr("jolt");
        if (StrUtil.isBlank(jolt)) {
            //未配置映射关系，直接返回null
            return driver;
        }
        logBasic("生成的jolt表达式"+jolt);

        List chainrSpecJSON = JsonUtil.toList(jolt, Map.class);
        Chainr chainr = Chainr.fromSpec(chainrSpecJSON);

        Object transformedOutput = chainr.transform(driver);
        return transformedOutput;
    }
}
